using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Collections.Specialized;

public partial class Employee : System.Web.UI.Page
{
    //String tempPass;

    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        try
        {
            // New connection
            Connection newCon = new Connection();
            SqlConnection con = newCon.ConStr;

            // open conneciton
            con.Open();

            // prepare command
            SqlCommand cmd = new SqlCommand("insert into Employee values(@username , @pass , @fullname , @bdate , @sex , @email , @address , @tel)", con);

            cmd.Parameters.AddWithValue("username", TextBox1.Text);
            cmd.Parameters.AddWithValue("pass", TextBox2.Text);
            cmd.Parameters.AddWithValue("fullname", TextBox3.Text);
            cmd.Parameters.AddWithValue("bdate" , TextBox4.Text) ;
            cmd.Parameters.AddWithValue("sex" , DropDownList1.SelectedValue) ;
            cmd.Parameters.AddWithValue("email" , TextBox6.Text) ;
            cmd.Parameters.AddWithValue("address" , TextBox7.Text) ;
            cmd.Parameters.AddWithValue("tel" , TextBox8.Text) ;

            // execute query
            cmd.ExecuteNonQuery();

            // reload gridview
            GridView1.DataBind();

            // close connection
            con.Close();
            Label1.Text = "";

            Membership.CreateUser(TextBox1.Text, TextBox2.Text);
            Roles.AddUserToRole(TextBox1.Text, "employee");
        }
        catch (Exception ex)
        {
            Label1.Text = "Cannot insert new record: " + ex.Message;
        }
    }

    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        DataTable dt = (DataTable)GridView1.DataSource ;
        String tempName = dt.Rows[e.RowIndex][0].ToString() ;
        Membership.DeleteUser(tempName);
    }
    protected void LinkButton1_Click(object sender, EventArgs e)
    {
        String tempName = ((GridViewRow)((Control)sender).Parent.Parent).Cells[0].Text;
        
        Response.Redirect("~/Admin/EmployeeDetails.aspx?username=" + tempName);
    }
}
